<template>
  <el-row>
    <el-col :span="8" :offset="8">
      <h2>用户注册</h2>
      <el-form  label-width="120px">
        <el-form-item label="用户名:">
          <el-input v-model="form.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="form.password" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="昵称:">
          <el-input v-model="form.nickname" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="区块链账户地址:">
          <el-input v-model="form.account_address" clearable></el-input>
        </el-form-item>
        <el-form-item label="角色:">
          <el-select v-model="form.role" placeholder="请选择:">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.role==1" label="银行地址">
          <el-input v-model="form.bank_address" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onRegister" style="margin-right: 60px">注册</el-button>
          <el-button type="primary" @click="goback" style="margin-right: 60px">返回</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

</template>

<script>
export default {
  name: "Register",
  data(){
    return{
      form:{
        username:'',
        password:'',
        nickname:'',
        account_address:'',
        role:'',
        bank_address:''
      },
      options:[{
        value:'0',
        label:'管理员'
      },{
        value:'1',
        label:'客户'
      },{
        value:'2',
        label:'银行'
      }]
    }
  },
  methods:{
    onRegister(){
      this.axios.post('/register',this.form)
      .then((res)=>{
        let code = res.data.code
        if (code == 200){
          alert("注册成功")
          this.$router.push('/Login')
        }else {
          alert(`注册失败，原因为${res.data.data}`)
        }
      })
      .catch((res)=>{
        console.log(res);
      })
    },
    goback() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
</style>